<?php
namespace App\Service\Member;

use Illuminate\Support\Facades\DB;

class OrderService extends BaseService
{
    //订单

    /**
     * 小区医护列表
     * @param array $pages【start，limit】
     * @param array $pas【uid，status(0所有，1待服务，2已完成，3已取消)】
     */
    public function yihuList($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbYihu)
            ->where('del', 0)
            //->where('status', '>', 2)
            ->where('did', $pas['uid']);
        if ($pas['status'] == 1) {
            $db = $db->where('is_close', 0)->where('status', '>=', 2);
        } else if ($pas['status'] == 2) {
            $db = $db->where('is_close', 0)->where('status', 6);
        } else if ($pas['status'] == 3) {
            $db = $db->where('is_close', 1);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return $this->toback();
        }
        $statuss = $this->yihu_statuss;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //订单
            $oone = (array) $this->dbConn($this->dbGc)
                ->where('type', 2)
                ->where('oid', $v['id'])
                ->first();
            $uname = '-';
            $uhead = '';
            $usex = '';
            $uage = '';
            //患者
            $patOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['uid'])
                ->first();
            if (!empty($patOne)) {
                if ($patOne['uname']) $uname = $patOne['uname'];
                if ($patOne['head']) {
                    if (substr($patOne['head'], 0, 4) == 'http') $uhead = $patOne['head'];
                    if (file_exists($patOne['head'])) $uhead = env('DOMAIN').'/'.$patOne['head'];
                }
                if ($patOne['sex']) $usex = $patOne['sex']==1 ? '男' : '女';
                if ($patOne['age']) $uage = $patOne['age'].'岁';
            }
            $status_name = array_key_exists($v['status'], $statuss) ? $statuss[$v['status']] : '';
            $arys[] = [
                'id' => !empty($oone) ? $oone['id'] : 0,
                'yw_type' => 2, 'yw_id' => $v['id'],
                'uname' => $uname, 'uhead' => $uhead,
                'usex' => $usex, 'uage' => $uage,
                'title' => $v['title'], 'serve_time' => $v['yytime'],
                'price' => toNumber($v['price']),
                'status' => $v['status'], 'status_name' => $status_name,
                'robtime' => $v['robtime'],
                'is_close' => $v['is_close'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 陪诊列表
     * @param array $pages【start，limit】
     * @param array $pas【uid，status(0所有，1待服务，2已完成，3已取消)】
     */
    public function peizhenList($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbPeizhen)
            ->where('del', 0)
            //->where('status', '>', 2)
            ->where('dot_uid', $pas['uid']);
        if ($pas['status'] == 1) {
            $db = $db->where('is_close', 0)->where('status', '>=', 2);
        } else if ($pas['status'] == 2) {
            $db = $db->where('is_close', 0)->where('status', 5);
        } else if ($pas['status'] == 3) {
            $db = $db->where('is_close', 1);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return $this->toback();
        }
        $statuss = $this->peizhen_statuss;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //订单
            $oone = (array) $this->dbConn($this->dbGc)
                ->where('type', 3)
                ->where('oid', $v['id'])
                ->first();
            $uname = '-';
            $uhead = '';
            $usex = '';
            $uage = '';
            //患者
            $patOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['pat_uid'])
                ->first();
            if (!empty($patOne)) {
                if ($patOne['uname']) $uname = $patOne['uname'];
                if ($patOne['head']) {
                    if (substr($patOne['head'], 0, 4) == 'http') $uhead = $patOne['head'];
                    if (file_exists($patOne['head'])) $uhead = env('DOMAIN').'/'.$patOne['head'];
                }
                if ($patOne['sex']) $usex = $patOne['sex']==1 ? '男' : '女';
                if ($patOne['age']) $uage = $patOne['age'].'岁';
            }
            $status_name = array_key_exists($v['status'], $statuss) ? $statuss[$v['status']] : '';
            $arys[] = [
                'id' => !empty($oone) ? $oone['id'] : 0,
                'yw_type' => 3, 'yw_id' => $v['id'],
                'uname' => $uname, 'uhead' => $uhead,
                'usex' => $usex, 'uage' => $uage,
                'title' => $v['title'], 'serve_time' => $v['start_time'],
                'price' => toNumber($v['price']),
                'status' => $v['status'], 'status_name' => $status_name,
                'robtime' => $v['robtime'],
                'is_close' => $v['is_close'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 小区医护详情
     * @param int $id
     */
    public function yihuOne($id)
    {
        $res = (array) $this->dbConn($this->dbYihu)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        //分类
        $cateOne = (array) $this->dbConn($this->dbCate)
            ->where('id', $res['type'])
            ->first();
        $cate_name = !empty($cateOne) ? $cateOne['title'] : '-';
        //患者
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $res['uid'])
            ->first();
        $uname = '-';
        $uage = '';
        $usex = '';
        $uphone = '';
        if (!empty($uOne)) {
            if ($uOne['uname']) $uname = $uOne['uname'];
            if ($uOne['age']) $usex = $uOne['sex']==1 ? '男' : '女';
            if ($uOne['phone']) $uphone = $uOne['phone'];
        }
        $arys = [
            'id' => $res['id'],
            'cate_name' => $cate_name,
            'uname' => $uname,
            'uage' => $uage,
            'usex' => $usex,
            'uphone' => $uphone,
            'serve_time' => $res['yytime'],
            'yydizhi' => $res['yydizhi'],
            'remark' => $res['remark'],
            'map_locate' => $res['yy_map'],
        ];
        return $this->toback(0, '', $arys);
    }

    /**
     * 陪诊详情
     * @param int $id
     */
    public function peizhenOne($id)
    {
        $res = (array) $this->dbConn($this->dbPeizhen)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        //患者
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $res['pat_uid'])
            ->first();
        $uname = '-';
        $uage = '';
        $usex = '';
        $uphone = '';
        if (!empty($uOne)) {
            if ($uOne['uname']) $uname = $uOne['uname'];
            if ($uOne['age']) $usex = $uOne['sex']==1 ? '男' : '女';
            if ($uOne['phone']) $uphone = $uOne['phone'];
        }
        //医院
        $yyOne = (array) $this->dbConn($this->dbYiyuan)
            ->where('id', $res['yiyuan'])
            ->first();
        $yy_name = !empty($yyOne) ? $yyOne['title'] : '';
        $arys = [
            'id' => $res['id'],
            'cate_name' => $res['title'],
            'uname' => $uname,
            'uage' => $uage,
            'usex' => $usex,
            'uphone' => $uphone,
            'start_time' => $res['start_time'],
            'end_time' => $res['end_time'],
            'yiyuan_name' => $yy_name,
            'price' => toNumber($res['price']),
            'remark' => $res['remark'],
            'map_locate' => $res['yy_map'],
        ];
        return $this->toback(0, '', $arys);
    }
}